Skip to main content

Hooks

The Viewer library provides several utility hooks that simplify building customizations and extensions. These hooks are part of the @promaton/scan-viewer package and are designed to enhance the development experience when working with the Viewer. Below is a detailed overview of the available hooks and contexts.


useShortcut

useShortcut(key, fn, deps?, options?): void

This hook allows you to register a keyboard shortcut tied to the lifecycle of a component.

Parameters

  • key: KeyFilter
    The key or combination of keys to listen for.
  • fn: Handler
    The function to execute when the shortcut is triggered.
  • deps?: DependencyList
    Optional dependencies for the hook.
  • options?:
    • excludeModifiers?: boolean
      Whether to exclude modifier keys (e.g., Shift, Ctrl) from the shortcut.

Returns

  • void

useViewSize

useViewSize(): Vector2

This hook retrieves the size of the current viewport. It is particularly useful in a react-three-fiber context.

Returns

  • Vector2: The dimensions of the viewport.

useIsViewerObjectLoaded

useIsViewerObjectLoaded(id): boolean

This hook checks whether an object with a specific ID has been loaded and displayed in the view at least once.

Parameters

  • id: string
    The ID of the object to check.

Returns

  • boolean: true if the object is loaded, otherwise false.

ViewContext

const ViewContext: Context<string>;

A React context that provides the ID of the view in which a component is rendered. Combine this with the useViews hook for additional information.


SceneObjectIdContext

const SceneObjectIdContext: Context<null | string>;

A context provider for the ViewerObject ID. This is useful for accessing the ID inside the children of a scene object.


These hooks and contexts are powerful tools for extending and customizing the Viewer library. By leveraging them, you can create highly interactive and dynamic user experiences.